package gov.cn.user.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import gov.cn.user.entity.UserInfo;
import gov.cn.user.exception.UserException;
import gov.cn.user.mapper.UserMapper;
import gov.cn.user.service.UserService;
import org.springframework.stereotype.Service;

import java.io.Serializable;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserInfo> implements UserService {
    @Override
    public void updById(UserInfo entity) throws UserException {
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<UserInfo>();
        //
        wrapper.lambda().eq(ObjectUtil.isNotEmpty(entity.getUserName()), UserInfo::getUserName, entity.getUserName());

        UserInfo user = this.baseMapper.selectOne(wrapper);
        if (ObjectUtil.isNotNull(user)) {
            if (!user.getUserId().equals(entity.getUserId())) {
                throw new UserException("用户名已存在");
            }
        }
        this.updateById(entity);
    }

    @Override
    public void add(UserInfo entity) throws UserException {
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<UserInfo>();
        //
        wrapper.lambda().eq(ObjectUtil.isNotEmpty(entity.getUserName()), UserInfo::getUserName, entity.getUserName());

        UserInfo user = this.baseMapper.selectOne(wrapper);
        if (ObjectUtil.isNotNull(user)) {
            throw new UserException("用户名已存在");
        }
        this.save(entity);
    }

    @Override
    public boolean checkName(UserInfo userInfo) {
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<UserInfo>();
        wrapper.lambda().eq(ObjectUtil.isNotEmpty(userInfo.getUserName()), UserInfo::getUserName, userInfo.getUserName());
        UserInfo user = this.baseMapper.selectOne(wrapper);
        if (ObjectUtil.isNotNull(user)) {
            if (ObjectUtil.isNotEmpty(userInfo.getUserId())) {
                if (!user.getUserId().equals(userInfo.getUserId())) {
                    return false;
                }
                return true;
            }
            return false;
        }
        return true;
    }


    @Override
    public IPage findByPage(int pageNum, int pageSize, UserInfo userInfo) {
        //验证
        if (pageNum < 1)
            pageNum = 1;

        if (pageSize < 1)
            pageSize = 10;

        if (ObjectUtil.isEmpty(userInfo)) {
            userInfo = new UserInfo();
        }

        // 查询条件
        QueryWrapper<UserInfo> wrapper = getuserInfoQueryWrapper(userInfo);
        IPage<UserInfo> page = new Page<>(pageNum, pageSize);
        page = this.page(page, wrapper);
        return page;
    }

    @Override
    public void enableById(Serializable id) throws UserException {
        UserInfo user = this.baseMapper.selectById(id);
        if (null == user) {
            throw new UserException("用户ID未查询到");
        }
        if (user.getEnabled().equals("0")) {
            user.setEnabled("2");
        } else {
            user.setEnabled("0");
        }
        this.baseMapper.updateById(user);
    }


    /**
     * 查询条件
     */
    private QueryWrapper<UserInfo> getuserInfoQueryWrapper(UserInfo userInfo) {
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<UserInfo>();
        // 
        wrapper.lambda().like(ObjectUtil.isNotEmpty(userInfo.getPy()), UserInfo::getPy, userInfo.getPy());
        // 
        wrapper.lambda().eq(ObjectUtil.isNotEmpty(userInfo.getUserName()), UserInfo::getUserName, userInfo.getUserName());
        return wrapper;
    }
}
